#include "projectdefinitions.h"

void shuffle(void);
void pick_card(void);
void pick_card_up(void);
void hide_dealer_card(void);
void pick_dealer_card(void);
void print_number(void);
void hit_stand(void);
void dealer_play(void);

///////////VARIABLES
	
	char card,number,symbol,player_sum=0,num_cards=0;
	char dealer_num, dealer_sym,a_high=0, player_tot;
	int remaining=0;
	char state=0; //0 for playing, 1 for wining, 2 for losing, 3 for blackjack, 4 for Tie
	char deck[52];

	char decide=1;

PROGRAM_INIT
	ACTIVATE_LCD
	//defining symbols
	LCD_DEF(1, 0x04, 0x0A, 0x11, 0x11, 0x11, 0x0A, 0x0E, 0x1F) //Diamonds          
	LCD_DEF(2, 0x11, 0x0A, 0x15, 0x04, 0x04, 0x15, 0x0A, 0x11) //Hearts          
	LCD_DEF(3, 0x15, 0x0A, 0x15, 0x0A, 0x15, 0x0A, 0x15, 0x0A) //Spades          
	LCD_DEF(4, 0x00, 0x00, 0x1B, 0x15, 0x11, 0x0A, 0x04, 0x00) //Trebols  
	LCD_DEF(5,0x1F, 0x11, 0x15, 0x15, 0x15, 0x11, 0x1F, 0x00) //back card
	LCD_DEF(6,0x17, 0x15, 0x15, 0x15, 0x15, 0x15, 0x17, 0x00) //10
	//welcome screen
	CLEAR_LCD
	LCD_TEXT(" Welcome to 21")
	LCD_XY(3,2)
	LCD_CHAR(0) // Display character #1
	LCD_TEXT("  ")
	LCD_CHAR(1) // Display character #2
	LCD_TEXT("  ")
	LCD_CHAR(2) // Display character #3
	LCD_TEXT("  ")
	LCD_CHAR(3) // Display character #4
	LCD_XY(2,3)
	LCD_TEXT(" For start B1 ")
	LCD_XY(2,4)
	LCD_TEXT(" For help B2")

	WAIT_SEC(1)

PROGRAM_START
	if (remaining<10)
	{
	shuffle();
	}
		
	//Playing Screen
	CLEAR_LCD
	LCD_TEXT(" P ")
	pick_card_up();
	pick_card_up();
	LCD_XY(2,3)
	LCD_TEXT("D ")
	pick_dealer_card();					//update dealer sum
	hide_dealer_card();
	LCD_XY(2,4)
	LCD_TEXT("<-HIT")
	LCD_XY(10,4)
	LCD_TEXT("STAND->")
	if(player_sum==21)  // BlackJack
	{
		CLEAR_LCD
		LCD_TEXT("BLACK JACK")
		state=3;
		WAIT_SEC(1)
	}
	else
	{
		//Gameplay
		while(state==0)
		{
			while((PINA & (1<<5))&(PINA & (1<<7)));//wait for left or rigth joystick
				{
				ON_JOYSTICK_LEFT
				{
					pick_card_up();
					WAIT_FOR_RELEASE_JOYSTICK_LEFT
				}
				ON_JOYSTICK_RIGHT
				{
					dealer_play();
					WAIT_FOR_RELEASE_JOYSTICK_RIGHT
				}
			}
		}
	num_cards=0;
	player_sum=0;
}
PROGRAM_END

void shuffle()
{
	LCD_DEF(7, 0x00, 0x1F, 0x0E, 0x04, 0x0E, 0x1F, 0x00, 0x00)// vertical  
	// Define special character #2  
	LCD_DEF(8, 0x00, 0x11, 0x1B, 0x1F, 0x1F, 0x1B, 0x11, 0x00)// horizontal  
	LCD_CLEAR
	LCD_TEXT("   SHUFFLING")  
	LCD_XY(6,2)  
	LCD_TEXT(" ")  
	LCD_CHAR(6) // Display character #1  
	WAIT_SEC(0.3)  
	LCD_XY(6,2)  
	LCD_TEXT(" ")  
	LCD_CHAR(7) // Display character #1  
	WAIT_SEC(0.3)  
	LCD_XY(6,2)  
	LCD_TEXT(" ")  
	LCD_CHAR(6) // Display character #1
	WAIT_SEC(0.3)  
	LCD_XY(6,2) 
	LCD_TEXT(" ")  
	LCD_CHAR(7) 
	WAIT_SEC(1)
	remaining=260;
	for(int i=0;i<52;i++)
	{
		deck[i]=5;
	}
}

void pick_card()
{
	do
	{card=RANDOM(1,52);
	}while(deck[card-1]==0);	//if there are no more cards keep choosing a random number
	
	if(deck[card-1]>0)
	{
		deck[card-1]=deck[card-1]-1;
		if(card<=13)
		{
			symbol=0;
		}
		if((card>13)&(card<=26))
		{
			symbol=1;
		}
		if((card>26)&(card<=39))
		{
			symbol=2;
		}
		if(card>39)
		{
			symbol=3;
		}
		number=card%13;
		print_number();
		LCD_CHAR(symbol)
		remaining=remaining-1;
		
	}
}

void print_number()
{
	
		if((number>1)&(number<10))
			LCD_NUMBER(number)
		if(number==10)
			LCD_CHAR(5)
		if(number==0)
			{LCD_CHAR('K'); number=10;}
		if(number==11)
			{LCD_CHAR('J');number=10;}
		if(number==12)
			{LCD_CHAR('Q');number=10;}
		if(number==1)
			{LCD_CHAR('A'); number=11;a_high=1;}
}

void pick_card_up()
{
	num_cards=num_cards+1;
	if(num_cards<5)
		LCD_XY(1+(num_cards)*3,1)
	if(num_cards>5)
		LCD_XY(1+(num_cards-4)*3,2)
	if(num_cards>9)
		LCD_XY(7,3)
	pick_card();
	player_sum=player_sum+number;
	if(player_sum>21) //chance to change an ace 11 for an ace 1
	{
		if(a_high>0)
		{
		player_sum=player_sum-10;
		a_high=0;	
		}
	}
	//LCD_XY(15,2)          
	//LCD_NUMBER(player_sum)  
	WAIT_SEC(.5)
	if(player_sum>21) //check for greater than 21
	{
		CLEAR_LCD
		LCD_TEXT("BUSTED")
		WAIT_SEC(1)
		num_cards=10;
 	}
}

void pick_dealer_card()
{
	LCD_XY(4,3)
	pick_card();
	dealer_num=number;//store value
	dealer_sym=symbol;//store symbol
}

void hide_dealer_card()
{
	LCD_CHAR(' ')
	LCD_CHAR(4)
	LCD_CHAR(4)
//	LCD_XY(15,3)
//	LCD_NUMBER(dealer_num)
}

void hit_stand()
{
	char prob_table[11]={0,31,39,56,58,62,69,77,85,92,100};//Probabilites for busting 
	//If below or equal to 11 0%, 31% for 12...
	char prob_bust;

	if(player_sum<=11)
		prob_bust=prob_table[0];
	else
		prob_bust=prob_table[player_sum-11];
	
	WAIT_SEC(2)	

	if(prob_bust<60)
		pick_card_up();
	else
		decide=0;
}

void dealer_play()
{
	player_tot=player_sum; //store total
	player_sum=dealer_num;
	num_cards=10;
	pick_card_up();
	//LCD_XY(15,3)
	//LCD_NUMBER(player_tot)
	WAIT_SEC(1)
	while(state==0)
	{
		if(player_sum>=player_tot)//dealer>player
		{
			LCD_CLEAR
			LCD_TEXT(" DEALER WINS ")
			WAIT_SEC(1)
		}
	}
}
